package com.lyfx.ly.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.lyfx.common.pojo.CatPojo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lyfx.ly.entity.ClassCategoryEntity;
import com.lyfx.ly.service.ClassCategoryService;
import com.lyfx.common.utils.PageUtils;
import com.lyfx.common.utils.R;



/**
 * **课程分类表**
 *
 * @author lsy
 * @email lsy@gmail.com
 * @date 2020-07-05 18:04:00
 */
@RestController
@RequestMapping("ly/classcategory")
public class ClassCategoryController {
    @Autowired
    private ClassCategoryService classCategoryService;

    /**
     * 查出所有分类以及子分类，已树形结构组装起来
     */
    @RequestMapping("/list/tree")
    @RequiresPermissions("ly:classcategory:list")
    public R list(){
        List<ClassCategoryEntity> entitys = classCategoryService.listWithTree();
        return R.ok().put("data", entitys);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    @RequiresPermissions("ly:classcategory:info")
    public R info(@PathVariable("catId") Long catId){
		ClassCategoryEntity classCategory = classCategoryService.getById(catId);

        return R.ok().put("data", classCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ly:classcategory:save")
    public R save(@RequestBody ClassCategoryEntity classCategory){
		classCategoryService.save(classCategory);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/beach/save")
    public R beachSave(@RequestBody List<CatPojo> catPojos){
        List<ClassCategoryEntity> collect = catPojos.stream().map(item -> {
            ClassCategoryEntity entity = new ClassCategoryEntity();
            BeanUtils.copyProperties(item, entity);
            return entity;
        }).collect(Collectors.toList());
        classCategoryService.saveBatch(collect);
        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ly:classcategory:update")
    public R update(@RequestBody ClassCategoryEntity classCategory){
		classCategoryService.updateCascade(classCategory);
        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/update/sort")
    @RequiresPermissions("ly:classcategory:update")
    public R updateSort(@RequestBody ClassCategoryEntity[] classCategory){
        classCategoryService.updateBatchById(Arrays.asList(classCategory));
        return R.ok();
    }



    /**
     * 删除
     * @RequestBody :获取请求体，必须发送post请求
     * springMVC自动将请求体的数据（json）转成 相应的实体
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ly:classcategory:delete")
    public R delete(@RequestBody Long[] catIds){
//		classCategoryService.removeByIds(Arrays.asList(catIds));
        //检查当前删除的菜单，是否被其他地方引用，如果引用，不能删除
        classCategoryService.removeMenuByids(Arrays.asList(catIds));
        return R.ok();
    }

}
